package com.mallcai.bigdata.ladon.dpl.mapper.system.provider;

import com.mallcai.bigdata.ladon.dpl.entity.system.RoleRlReport;
import com.mallcai.bigdata.ladon.dpl.mapper.BaseSqlProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author gst
 * @date 2019-12-30 20:36
 */
public class RoleRlReportSqlProvider extends BaseSqlProvider<RoleRlReport> {
    public static final String TB_ROLE_RL_REPORT = "sys_role_rl_report";

    /**
     * 获取表名
     *
     * @return String
     */
    @Override
    public String getTableName() {
        return TB_ROLE_RL_REPORT;
    }

    /**
     * 获取插入数据的字段列表
     *
     * @return Map
     */
    @Override
    public Map<String, String> getInsertColumns() {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("role_id", "#{roleId}");
        map.put("report_id", "#{reportId}");
        return map;
    }

    /**
     * 获取更新操作的字段列表
     *
     * @return Map
     */
    @Override
    public Map<String, String> getUpdateColumns() {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("reportId", "report_id");
        map.put("roleId", "role_id");
        return map;
    }

    public String getByRoleIds(@Param("roleIds") List<Long> roleIds) {
        StringBuilder sb = new StringBuilder();
        sb.append("archive = 0");
        if (roleIds != null && roleIds.size() > 0) {
            sb.append(" AND role_id IN (");
            for (int i = 0; i < roleIds.size(); i++) {
                if (i > 0) {
                    sb.append(", ");
                }
                sb.append(roleIds.get(i));
            }
            sb.append(" )");
        } else {
            sb.append(" AND 1 = 0");
        }
        return new SQL() {
            {
                SELECT("*");
                FROM(getTableName());
                WHERE(sb.toString());
            }
        }.toString();
    }

    public String getByReportId(@Param("reportId") Long reportId) {
        return new SQL() {
            {
                SELECT("tb_rl.*, tb_role.name AS roleName ");
                FROM(getTableName() + " AS tb_rl ");
                LEFT_OUTER_JOIN(RoleSqlProvider.TB_ROLE + " tb_role ON tb_role.id = tb_rl.role_id ");
                WHERE("tb_rl.archive = 0 AND tb_rl.report_id = #{reportId}");
            }
        }.toString();
    }
}
